<?php

class Photos extends CActiveRecord
{
  public $upload;

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return 'photos';
	}

	public function rules()
	{
		return array(
			array('content', 'required'),
      array('upload', 'file', 'types' => 'jpg, png, gif', 'on' => 'create'),
      array('upload', 'file', 'on'=>'update', 'types' => 'jpg, png, gif','allowEmpty' => true),
		);
	}

	public function relations()
	{
		return array(
			'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
		);
	}

	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'content' => 'Content',
			'created_at' => 'Created At',
			'user_id' => 'User',
		);
	}

	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('user_id',$this->user_id);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

  public function getProfilePicture($user_id)
  {
    $criteria = new CDbCriteria;

    $criteria->condition = "user_id = ".$user_id. " AND profile_picture = 1";
    $photo = Photos::model()->find($criteria);

    if($photo){
      return $photo->content;
    }
    return false;
  }
}